package com.da.lon.wang.xlg.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.adapter.StoreListAdapter;
import com.da.lon.wang.xlg.bean.StoreListResult;
import com.da.lon.wang.xlg.bean.StoreListResult.StoreInfo;
import com.da.lon.wang.xlg.constants.XLGConstants;
import com.da.lon.wang.xlg.http.HttpLoader;
import com.da.lon.wang.xlg.http.HttpLoader.OnWebLoadListener;
import com.da.lon.wang.xlg.pulltorefresh.library.PullToRefreshListView;
import com.da.lon.wang.xlg.util.BuyUtil;
import com.da.lon.wang.xlg.util.DialogUtil;
import com.da.lon.wang.xlg.util.XLGDataUtils;
import com.umeng.analytics.MobclickAgent;

import net.tsz.afinal.http.AjaxParams;

import java.util.ArrayList;

public class StoreListActivity extends Activity implements OnClickListener {

    TextView title;
    ImageButton left;
    PullToRefreshListView listView;
    BuyUtil util;
    DialogUtil dialogUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store_list);
        util = new BuyUtil(this);
        dialogUtil = new DialogUtil(this);
        title = (TextView) findViewById(R.id.title);
        title.setText("欧咖体验店");
        left = (ImageButton) findViewById(R.id.ouka_home_buyer_left);
        left.setOnClickListener(this);
        listView = (PullToRefreshListView) findViewById(R.id.store_list);
        getData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    private void getData() {

        final double[] coordinates = util.getCoordinates();
        HttpLoader<StoreListResult> loder = new HttpLoader<>(this,
                XLGConstants.URL + "Store-getStoreList", StoreListResult.class);
        AjaxParams params = new AjaxParams();
        params.put(
                "token",
                XLGDataUtils.encrypt2MD5("Store" + XLGConstants.SECRET
                        + "getStoreList"));
        params.put("needCoordinates", "1");
        params.put("latitude", coordinates[0] + "");
        params.put("longitude", coordinates[1] + "");
        loder.loadData(params);
        loder.setManagerListener(new OnWebLoadListener<StoreListResult>() {

            @Override
            public void OnStart() {
                dialogUtil.showDialog();
            }

            @Override
            public void OnError(String error) {
                dialogUtil.dismissDialog();
            }

            @Override
            public void onSuccess(StoreListResult t) {
                dialogUtil.dismissDialog();
                if (t != null && "000000".equals(t.getCode())) {
                    final ArrayList<StoreInfo> datas = t.getResult();
                    StoreListAdapter adapter = new StoreListAdapter(datas,
                            coordinates, StoreListActivity.this);
                    listView.setAdapter(adapter);
                    listView.setOnItemClickListener(new OnItemClickListener() {

                        @Override
                        public void onItemClick(AdapterView<?> parent,
                                                View view, int position, long id) {
                            Intent intent = new Intent(StoreListActivity.this,
                                    StoreInfoActivity.class);
                            Bundle bundle = new Bundle();
                            bundle.putString("store_id", datas
                                    .get(position - 1).getStore_id());
                            bundle.putString("store_name",
                                    datas.get(position - 1).getStore_name());
                            intent.putExtras(bundle);
                            startActivity(intent);
                        }
                    });
                }
            }
        });


    }

    @Override
    public void onClick(View v) {
        finish();
    }
}
